package com.ruoyi.yl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.yl.domain.YlEmployeeIds;
import com.ruoyi.yl.domain.YlOutDrug;
import com.ruoyi.yl.domain.YlStockOut;
import com.ruoyi.yl.mapper.OutManageMapper;
import com.ruoyi.yl.mapper.YlDrugEmployeeMapper;
import com.ruoyi.yl.mapper.YlStockOutMapper;
import com.ruoyi.yl.service.OutManageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: linKe
 * @Date: 2022/2/19 9:29
 * @FileName: DrugInfoMaintenanceServiceImpl
 * @version: 1.0
 * @Description: 出库管理
 */
@Service
@Transactional(readOnly = true)
public class OutManageServiceImpl implements OutManageService {

    @Resource
    private OutManageMapper outManageMapper;

    @Resource
    private YlDrugEmployeeMapper ylDrugEmployeeMapper;

    @Resource
    private YlStockOutMapper ylStockOutMapper;
    /**
     * 查看所有出库信息
     */
    @Override
    public List<YlStockOut> getAll() {
        List<YlStockOut> all = outManageMapper.selectList(null);

//        for (YlStockOut out:all){
//            out.setOutWorker(1L);
//            out.setTableMaker(1L);
//            YlEmployeeIds allEmployeeId = outManageMapper.getAllEmployeeId(out.getId());
//            //获取出库人员名称
//            String outWorker=outManageMapper.getEmployeeNameById(allEmployeeId.getStockId());
//            /// out.getOutWorker().setName(outWorker);
//
//            //获取制表人员名称
//            String tableMaker=outManageMapper.getEmployeeNameById(allEmployeeId.getTableMakerId());
//            /// out.getTableMaker().setName(tableMaker);
//        }
        return all;
    }

    /**
     * 根据id查出库信息
     * 出库信息和角色信息
     */
    @Override
    public YlOutDrug getOneById(Long id) {
        YlOutDrug outDrug = outManageMapper.getOneById(id);
        YlStockOut out = ylStockOutMapper.selectById(outDrug.getOutId());

        out.setTableMaker(1L);
        out.setOutWorker(1L);
        out.setCheckWorker(1L);
        YlEmployeeIds allEmployeeId = outManageMapper.getAllEmployeeId(id);
        //获取入库人员名称
        String outWorker=outManageMapper.getEmployeeNameById(allEmployeeId.getStockId());
        /// out.getOutWorker().setName(outWorker);

        //获取制表人员名称
        String tableMaker=outManageMapper.getEmployeeNameById(allEmployeeId.getTableMakerId());
        /// out.getTableMaker().setName(tableMaker);

        //获取审核人员名称
        String checkWorker=outManageMapper.getEmployeeNameById(allEmployeeId.getCheckWorkerId());
        ///out.getCheckWorker().setName(checkWorker);
        return outDrug;
    }
    /**
     * 根据审核状态查询出库信息
     */
    @Override
    public List<YlStockOut> getOutsByCheckStatus(Long id) {
        List<YlStockOut> list = outManageMapper.selectList(new QueryWrapper<YlStockOut>().eq("check_status",id));
        for (YlStockOut out:list){
             out.setOutWorker(1L);
             out.setTableMaker(1L);
           /// YlEmployeeIds allEmployeeId = outManageMapper.getAllEmployeeId(out.getId());
            //获取出库人员名称
           /// String outWorker=outManageMapper.getEmployeeNameById(allEmployeeId.getStockId());
            /// out.getOutWorker().setName(outWorker);
            //获取制表人员名称
           /// String tableMaker=outManageMapper.getEmployeeNameById(allEmployeeId.getTableMakerId());
            /// out.getTableMaker().setName(tableMaker);
        }
        return list;
    }
    /**
     * 根据入库类型查询出库信息
     */
    @Override
    public List<YlStockOut> getOutsByType(Long id) {
        List<YlStockOut> list = outManageMapper.selectList(new QueryWrapper<YlStockOut>().eq("out_type",id));
        for (YlStockOut out:list){
            out.setOutWorker(1L);
            out.setTableMaker(1L);
            ///YlEmployeeIds allEmployeeId = outManageMapper.getAllEmployeeId(out.getId());
            //获取出库人员名称
            ///String outWorker=outManageMapper.getEmployeeNameById(allEmployeeId.getStockId());
            /// out.getOutWorker().setName(outWorker);
            //获取制表人员名称
            ///String tableMaker=outManageMapper.getEmployeeNameById(allEmployeeId.getTableMakerId());
            /// out.getTableMaker().setName(tableMaker);
        }
        return list;
    }
    /**
     * 根据输入框模糊查询出库信息
     */
    @Override
    public List<YlStockOut> getOutsByLike(String dsc) {
        List<YlStockOut> list = outManageMapper.getOutsByLike(dsc);
        for (YlStockOut out:list){
            out.setOutWorker(1L);
            out.setTableMaker(1L);
            YlEmployeeIds allEmployeeId = outManageMapper.getAllEmployeeId(out.getOutWorker());
            //获取出库人员名称
            String outWorker=outManageMapper.getEmployeeNameById(allEmployeeId.getStockId());
            /// out.getOutWorker().set(outWorker);
            //获取制表人员名称
            String tableMaker=outManageMapper.getEmployeeNameById(allEmployeeId.getTableMakerId());
            /// out.getTableMaker().setName(tableMaker);
        }
        return list;
    }


    /**
     * 添加出库信息
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public int addOut(YlStockOut out) {
        return outManageMapper.insert(out);
    }
}
